package team.seekers.hula.course.resource.problem.entity.dao;

import com.baomidou.mybatisplus.annotation.*;
import lombok.*;
import org.springframework.beans.BeanUtils;
import team.seekers.hula.common.entity.BaseDO;
import team.seekers.hula.common.utils.BaseConvertUtils;
import team.seekers.hula.course.resource.problem.entity.dto.ProblemDTO;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * (EduProblem)实体类
 *
 * @author GH
 * @since 2022-05-02 16:05:19
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = true)
@TableName(ProblemDOField.TABLE_NAME)
public class ProblemDO extends BaseDO {

    private static final long serialVersionUID = 1L;

    @TableId(value = ProblemDOField.ID, type = IdType.ASSIGN_ID)
    private String id;
    /**
     * 题目公开在题库, 0否, 1是
     */
    @TableField(ProblemDOField.IS_PUBLIC)
    private Integer isPublic;
    /**
     * 题目添加者id
     */
    @TableField(ProblemDOField.USER_ID)
    private String userId;
    /**
     * 题目类型（选择、判断、问答）
     */
    @TableField(ProblemDOField.TYPE)
    private Integer type;
    /**
     * 题目内容
     */
    @TableField(ProblemDOField.P_DESCRIPTION)
    private String description;
    /**
     * 题目提交数
     */
    @TableField(ProblemDOField.SUBMIT_NUM)
    private Integer submitNum;
    /**
     * 题目通过数
     */
    @TableField(ProblemDOField.ACCEPT_NUM)
    private Integer acceptNum;

    /**
     * 题目选项集合，例如Axxx,Bxxx,Cxxx,Dxxx
     */
    @TableField(ProblemDOField.OPTIONS)
    private String options;

    /**该题目的答案：
     *     1.问答题为字符串；
     *     2.选择题为二进制表示的整数（例如答案为ABD，二进制为1101，整数为13）
     **/
    @TableField(ProblemDOField.ANSWER)
    private String answer;

    @TableField(ProblemDOField.DELETED)
    @TableLogic(value = "0", delval = "1")
    private Integer deleted;

    @TableField(value = ProblemDOField.GMT_CREATE, fill = FieldFill.INSERT)
    private Date gmtCreate;

    @TableField(value = ProblemDOField.GMT_MODIFIED, fill = FieldFill.INSERT_UPDATE)
    private Date gmtModified;

    public static ProblemDO fromDTO(ProblemDTO problemDTO){
        ProblemDO problemDO = new ProblemDO();
        BeanUtils.copyProperties(problemDTO,problemDO);
        return problemDO;
    }

    public static ProblemDTO toDTO(ProblemDO problemDO){
        ProblemDTO problemDTO = new ProblemDTO();
        BeanUtils.copyProperties(problemDO,problemDTO);
        problemDTO.setOptions(Arrays.asList(problemDO.options.split(",")));
        return problemDTO;
    }

    public static List<ProblemDTO> toListDTO(List<ProblemDO> problemDOList){
        ArrayList<ProblemDTO> problemDTOS = new ArrayList<>();
        for (ProblemDO problemDO : problemDOList) {
            ProblemDTO problemDTO = new ProblemDTO();
            BeanUtils.copyProperties(problemDO,problemDTO);

            problemDTO.setOptions(BaseConvertUtils.stringToList(problemDO.options));
            problemDTOS.add(problemDTO);
        }
        return problemDTOS;
    }
}
